package com.foo.FileParser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.ejb.Stateless;

import com.foo.DeCompressData.DeCompress;

/**
 * Session Bean implementation class FileParser
 */
@Stateless
public class FileParser implements FileParserRemote, FileParserLocal {

	private DeCompress objUnArchive;
    /**
     * Default constructor. 
     */
    public FileParser() {
        
    }
    private String checkArchive(String fileName)
    {
    	boolean isArchived = isArchive(fileName);
    	if(isArchived)
    	{
    		objUnArchive = new DeCompress();
			
			objUnArchive.unzipFile(fileName);
			
			int index = fileName.lastIndexOf(".");
			return fileName.substring(0,index) + ".txt";
    	}
    	else
    		return fileName;
    }
    private boolean isArchive(String fileName)
    {
    	try
    	{
    		if(fileName.endsWith(".zip"))
    		{
    			return true;
    		}
    		else if(fileName.endsWith(".txt"))
    		{
    			return false;
    		}
    		else
	    	{
	    		throw new Exception("Invalid File entered");
	    	}
    	}
    	catch(Exception e)
    	{
    		System.out.println(e.getMessage());
    	}
    	return false;	
    }
    private Date convertToDate(String date)
    {
    	try
    	{
    	 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    	 Date convertedDate = (Date) dateFormat.parse(date);
    	 return convertedDate;
    	}
    	catch(Exception e)
    	{
    		System.out.println("Error while parsing!!!");
    		return null;
    	}

    }
	@Override
	public String readData(String fileName, Date startDate, Date endDate,
			String tickerSymbol) {
		try
    	{
    		String fileNameToRead = checkArchive(fileName);
    		BufferedReader reader = new BufferedReader(new FileReader(fileNameToRead));
    		String fileData;
    		Date date;
    		StringBuilder parsedData = new StringBuilder();
    		while ((fileData = reader.readLine()) != null) {
    			String[] str = fileData.split(",");
    			date  = convertToDate(str[0]);
    			if (str[1].equals(tickerSymbol) && (date.compareTo(startDate)>=0) && (date.compareTo(endDate)<=0)) {
    				parsedData.append(fileData);
    				parsedData.append(":");
    			}
    		}
    		return parsedData.toString();
    	}
    	catch(Exception e)
    	{
    		System.out.println(e.getMessage());
    		return null;
    	}  
	}
	@Override
	public String readData(String fileName, Date startDate, Date endDate) {
		try
    	{
    		String fileNameToRead = checkArchive(fileName);
    		BufferedReader reader = new BufferedReader(new FileReader(fileNameToRead));
    		String fileData;
    		Date date;
    		StringBuilder parsedData = new StringBuilder();
    		while ((fileData = reader.readLine()) != null) {
    			String[] str = fileData.split(",");
    			date  = convertToDate(str[0]);
    			if ((date.compareTo(startDate)>=0) && (date.compareTo(endDate)<=0)) {
    				parsedData.append(fileData);
    				parsedData.append(":");
    			}
    		}
    		
    		//Delete the file already read..
    		if(new File(fileNameToRead).exists())
    		{
    			if(fileNameToRead.endsWith(".txt"))
    			{
    				new File(fileNameToRead).delete();
    			}
    		}
    		return parsedData.toString();
    	}
    	catch(Exception e)
    	{
    		System.out.println(e.getMessage());
    		return null;
    	} 
	}
   
    
   
}
